package com.biostime.qdingding.modules.sys.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.sys.entity.Area;
import com.biostime.qdingding.modules.sys.service.AreaService;

/**
 * 区域Controller
 * @author phil
 * @version 2015-11-06
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/area")
public class AreaController extends BaseController {

	@Autowired
	private AreaService areaService;
	
	@ModelAttribute
	public Area get(@RequestParam(required=false) String id) {
		Area entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = areaService.get(id);
		}
		if (entity == null){
			entity = new Area();
		}
		return entity;
	}
	
	@RequestMapping(value = {"ajax"})
	public void ajax(HttpServletRequest request, HttpServletResponse response) {
		try {
			String method = request.getParameter("method");
			if(method != null && method.equalsIgnoreCase("getchilds")){
				String parentId = request.getParameter("parentid");
				List<Area> childs = areaService.getByParentId(parentId);
				response.getWriter().write(String.format("<option value=\"%s\">%s</option>\n","","请选择..."));
				for(Area area : childs){
					response.getWriter().write(String.format("<option value=\"%s\">%s</option>\n", area.getId(),area.getName()));
				}
			}else if(method != null && method.equalsIgnoreCase("getarea")){
				String parentId = request.getParameter("parentid");
				List<Area> childs = areaService.getByParentId(parentId);
				response.getWriter().write(String.format("<option value=\"%s\">%s</option>\n","","请选择..."));
				for(Area area : childs){
					response.getWriter().write(String.format("<option value=\"%s\">%s</option>\n", area.getId(),area.getName()));
				}
			}else{
				response.getWriter().write("缺少method参数");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequiresPermissions("sys:area:view")
	@RequestMapping(value = {"list", ""})
	public String list(Area area, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Area> page = areaService.findPage(new Page<Area>(request, response), area); 
		model.addAttribute("page", page);
		return "modules/sys/areaList";
	}

	@RequiresPermissions("sys:area:view")
	@RequestMapping(value = "form")
	public String form(Area area, Model model) {
		List<Area> parentAreas = areaService.getParentsTree();
		
		model.addAttribute("area", area);
		model.addAttribute("parentAreas",parentAreas);
		return "modules/sys/areaForm";
	}

	@RequiresPermissions(value={"sys:area:edit","sys:area:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Area area, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, area)){
			return form(area, model);
		}
		areaService.save(area);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/sys/area/?repage";
	}
	
	@RequiresPermissions("sys:area:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Area area, RedirectAttributes redirectAttributes) {
		
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				areaService.delete(new Area(id));
			}
		}else{
			areaService.delete(area);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/sys/area/?repage";
	}

}